﻿@model CouponListModel
@using Urs.Services.Security

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">开始日期</label>
                    <div class="layui-input-inline">
                        <input type="text" name="StartTime" id="StartTime" placeholder="yyyy-MM-dd" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">结束日期</label>
                    <div class="layui-input-inline">
                        <input type="text" name="EndTime" id="EndTime" placeholder="yyyy-MM-dd" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn" lay-submit lay-filter="LAY-app-contlist-search">搜索</button>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn  layui-btn-normal layuiadmin-btn-tags" data-type="add">添加</button>
                </div>
            </div>
        </div>
        <div class="layui-card-body">
            <table id="LAY-topic-list" lay-filter="LAY-topic-list"></table>
            <script type="text/html" id="layuiadmin-app-cont-tagsbar">
                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
            </script>
        </div>
    </div>
</div>

<script type="text/html" id="amountTpl">
    {{#  if(d.IsAmountLimit){ }}
    <button class="layui-btn layui-btn-xs">{{d.Amount}}</button>
    {{#  } else { }}
    <button class="layui-btn layui-btn-primary layui-btn-xs">无限</button>
    {{#  } }}
</script>
<script type="text/html" id="dateTpl">
    {{layui.util.toDateString(d.StartTime,'yyyy年MM月dd日')}} - {{ layui.util.toDateString(d.EndTime,'yyyy年MM月dd日')}}
</script>
<script>
    layui.config({
        base: '../../../layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index','table'], function () {
        var index = layer.load(2, { shade: false });
        var table = layui.table;

        //分类管理
        table.render({
            elem: '#LAY-topic-list'
            , url: '@Html.Raw(Url.Action("ListJson", "Coupon"))' //模拟接口
            ,method:'post'
            , page: true //开启分页
            , even: true
            , limit: 15
            , limits: [15,30,50,100]
            , cols: [[
                { type: 'numbers', fixed: 'left' }
                , { field: 'Id', width: 100, title: 'ID' }
                , { field: 'Title', title: '标题', minWidth: 100 }
                , { field: 'Value', title: '金额', width: 100 }
                , { field: 'StartTime', title: '有效时间', minWidth: 150,templet:'#dateTpl' }
                , { field: 'MinimumConsumption', title: '最低消费', width: 100 }
                , { field: 'Amount', title: '数量', width: 100, templet: '#amountTpl', }
                , { field: 'UsedAmount', title: '已使用数量', width: 100 }
                , { title: '操作', width: 150, align: 'center', fixed: 'right', toolbar: '#layuiadmin-app-cont-tagsbar' }
            ]]
            , id: 'testReload'
            , text: { none: '一条数据也没有^_^' }
            , done: function () {
                layer.close(index);
            }

        });
        //监听搜索
        layui.form.on('submit(LAY-app-contlist-search)', function (data) {
            var field = data.field;
            //执行重载
            table.reload('testReload', {
                page: {
                    curr: 1 //重新从第 1 页开始
                }
                , where: field
            }, 'data');
        });
        //监听工具条
        table.on('tool(LAY-topic-list)', function (obj) {
            var data = obj.data;
            if (obj.event === 'del') {
                layer.confirm('确定删除？', function (index) {
                    obj.del();
                    $.post('@Url.Action("Delete")', { id: obj.data.Id });
                    layer.close(index);
                });

            } else if (obj.event === 'edit') {
                var tr = $(obj.tr);
                layer.open({
                        type: 2
                        , title: '编辑'
                        , content:  '@Url.Action("Edit")?id=' + data.Id
                        , area: ['800px', '600px']
                        , btn: ['确定', '取消']
                        , yes: function (index, layero) {
                            //点击确认触发 iframe 内容中的按钮提交
                            var submit = layero.find('iframe').contents().find("#layuiadmin-app-form-submit");
                            submit.click();
                        }
                    });
            }
        });
        var $ = layui.$, active = {
             add: function () {
                    layer.open({
                        type: 2
                        , title: '编辑'
                        , content: '@Url.Action("Edit")'
                        , area: ['800px', '600px']
                        , btn: ['确定', '取消']
                        , yes: function (index, layero) {
                            //点击确认触发 iframe 内容中的按钮提交
                            var submit = layero.find('iframe').contents().find("#layuiadmin-app-form-submit");
                            submit.click();
                        }
                    });
                }
        }
        $('.layui-btn.layuiadmin-btn-tags').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>

